package com.zuqiuzhandui.liveing.net;

import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.zuqiuzhandui.liveing.model.WXResultModel.GetAccessTokenWXResM;
import com.zuqiuzhandui.liveing.model.WXResultModel.UserInfoWXResM;
import com.zuqiuzhandui.liveing.myInterfacce.Constant;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;

/**
 * Created by yq on 2016-10-11.
 */

public class WXMethods {
    String WX_BASE_URL = "https://api.weixin.qq.com/";
    public Retrofit retrofit;
    public WxApi wxService;

    private WXMethods() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(new StethoInterceptor())
                .connectTimeout(Constant.DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(WX_BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        wxService = retrofit.create(WxApi.class);
    }


    public static class singletionHolder {
        private static WXMethods INSTANCE = new WXMethods();

    }

    public static WXMethods getInstance() {
        return singletionHolder.INSTANCE;
    }


    public void getAccessToken(Subscriber<GetAccessTokenWXResM> subscriber, String appid, String secret, String code, String grantType) {
        ObservableDecorator.decorate(wxService.getAccessToken(appid, secret, code, grantType))
                .subscribe(subscriber);
    }

    public void getUserInfo(Subscriber<UserInfoWXResM> subscriber, String accessToken, String openid) {
        ObservableDecorator.decorate(wxService.getUserInfo(accessToken, openid))
                .subscribe(subscriber);
    }


}
